﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace Sunboxsoft.ASMS.Client.Domain
{
    public class RemoteInfo
    {
        public static string ASMSWebServiceURL { get; set; }
        public static string BasesWebServiceURL { get; set; }

        public static bool IsOffline { get; set; }

        public static CookieContainer CookieContainer { get; private set; }

        static object s_lockObject = new object();
        static List<ASMSWebServiceClient> s_asmsClients = new List<ASMSWebServiceClient>();

        public static WebRef.ASMSWebService GetASMSService()
        {
            lock (s_lockObject)
            {
                ASMSWebServiceClient client = null;
                for (int index = 0; index < s_asmsClients.Count; index++)
                {
                    if (s_asmsClients[index].IsRunning == false)
                    {
                        client = s_asmsClients[index];
                        break;
                    }
                }

                if (client == null)
                {
                    if (s_asmsClients.Count > 5)
                    {
                        throw new Exception("client instance count more then 5");
                    }
                    if (CookieContainer == null)
                    {
                        CookieContainer = new CookieContainer();
                    }
                    WebRef.ASMSWebService service = new WebRef.ASMSWebService();
                    service.Url = RemoteInfo.ASMSWebServiceURL;
                    service.CookieContainer = CookieContainer;

                    client = new ASMSWebServiceClient()
                    {
                        IsRunning = true,
                        Service = service
                    };
                    s_asmsClients.Add(client);
                }

                return client.Service;
            }
        }

        public static void ReleaseASMSService(WebRef.ASMSWebService service)
        {
            lock (s_lockObject)
            {
                for (int index = 0; index < s_asmsClients.Count; index++)
                {
                    if (s_asmsClients[index].Service == service)
                    {
                        s_asmsClients[index].IsRunning = false;
                        return;
                    }
                }
            }
        }

        public static BasesWebRef.BasesWebService CreateBasesWebService()
        {
            if (CookieContainer == null)
            {
                CookieContainer = new CookieContainer();
            }
            BasesWebRef.BasesWebService service = new BasesWebRef.BasesWebService();
            service.Url = RemoteInfo.BasesWebServiceURL;
            service.CookieContainer = CookieContainer;
            return service;
        }
    }

    class ASMSWebServiceClient
    {
        public bool IsRunning;
        public WebRef.ASMSWebService Service;
    }
}
